//
//  LTChooseCLassTermView.m
//  LearnTeacher
//
//  Created by zyl on 2024/4/12.
//

#import "LTChooseCLassTermView.h"

@interface LTChooseCLassTermView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,assign)BOOL open;

@property(nonatomic ,strong)UIButton *selectedBtn;

@end
@implementation LTChooseCLassTermView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    self.selectedBtn =  ({
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font =RFontWithSize(12);
        [button setImage:[UIImage imageNamed:@"ic_class_arrow_down"] forState:UIControlStateNormal];
        button.adjustsImageWhenHighlighted = NO;
        [button addTarget:self action:@selector(openView) forControlEvents:UIControlEventTouchUpInside];
       
        button;
        
    });
    [ self.selectedBtn setAllCornersWithCornerRadius:10];
    self.selectedBtn.backgroundColor = rgba(255, 255, 255, 0.10);
    [self.selectedBtn setFrame:CGRectMake(4, 0, 130, 25)];
    [self.selectedBtn setTitleColor:LTWhiteColor forState:UIControlStateNormal];
    [self addSubview:self.selectedBtn];
    [self addSubview:self.tableView];
    return self;
}
- (void)openView
{
    self.tableView.hidden = NO;
    self.selectedBtn.hidden = YES;
    self.height = self.tableView.height;
}
- (void)closeView
{
    self.tableView.hidden = YES;
    self.selectedBtn.hidden = NO;
    self.height = 25;
}
- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    [self.tableView reloadData];
    self.tableView.height = dataArray.count*31;
}
- (void)setSelectedTerm:(NSInteger)selectedTerm
{
    _selectedTerm = selectedTerm;
    [self.selectedBtn setTitle:self.dataArray[selectedTerm] forState:UIControlStateNormal];
    [self.selectedBtn layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:7];
}
#pragma mark -UITableViewDelegate,UITableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@""];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    
    [cell removeAllSubviews];
    UIButton *clickBtn =  ({
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font =RFontWithSize(12);
        button.userInteractionEnabled = NO;
        [button setTitle:self.dataArray[indexPath.row] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"ic_class_arrow_down"] forState:UIControlStateNormal];
        button.adjustsImageWhenHighlighted = NO;
        [button layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:7];
        button;
        
    });
    UIColor *bgColor= LTClearColor;
    UIColor *tColor= LTTextBlack;
    clickBtn.imageView.hidden = YES;
    if (indexPath.row == self.selectedTerm) {
        clickBtn.imageView.hidden = NO;
        bgColor = rgba(41, 121, 255, 1);
        tColor = LTWhiteColor;
    }
    [clickBtn setAllCornersWithCornerRadius:10];
    clickBtn.backgroundColor = bgColor;
    [clickBtn setTitleColor:tColor forState:UIControlStateNormal];
    [clickBtn setFrame:CGRectMake(4, 4, 130, 25)];
    [cell addSubview:clickBtn];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return 31;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"HHHHHHHHHHH%f",self.tableView.contentSize.height);
    if ( self.selectedTerm != indexPath.row) {
        self.selectedTerm = indexPath.row;
        [self.tableView reloadData];
        if (self.block) {
            self.block( self.selectedTerm);
        }
    }
    
    [self closeView];
    
}
#pragma mark -getters and setters
-(UITableView *)tableView{
    
    if (!_tableView){
        
        _tableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 138,30) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        _tableView.bounces = NO;
        _tableView.hidden = YES;
        [_tableView setBackgroundColor:LTWhiteColor];
        [_tableView setSeparatorColor:LTClearColor];
        [_tableView setAllCornersWithCornerRadius:10];

    }
    
    return _tableView;
}
@end
